package com.wanmait.studentmangers.dao;

import com.wanmait.studentmangers.util.DBHelper;
import com.wanmait.studentmangers.vo.Addressinfo;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository
public class AddressinfoDaoImpl implements AddressinfoDao{


    @Override
    public void insert(Addressinfo addressinfo) {

    }

    @Override
    public boolean login(Addressinfo addressinfo) {
        return false;
    }

    @Override
    public List getAll() {
        return null;
    }

    @Override
    public void add(Addressinfo addressinfo) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        conn = DBHelper.getConnection();

        String sql = "insert into addressinfo(province,city) values(?,?)";

        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, addressinfo.getProvince());
            pstmt.setString(2, addressinfo.getCity());
            pstmt.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);

        }finally{
            DBHelper.close(conn,pstmt,null);
        }
    }

    @Override
    public List<Addressinfo> getList(){
        List<Addressinfo> list = new ArrayList<Addressinfo>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        conn = DBHelper.getConnection();
        String sql = "select * from addressinfo";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while(rs.next()){
                Addressinfo addressinfo = new Addressinfo();
                addressinfo.setId(rs.getInt("id"));
                addressinfo.setProvince(rs.getString("province"));
                addressinfo.setCity(rs.getString("city"));
                list.add(addressinfo);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBHelper.close(conn,pstmt,rs);
        }
        return list;

    }
}



